/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kernel;

import gui.Factory;
import java.io.*;
import java.util.List;
import org.jdom.Element;

/**
 *
 * @author alvaro
 */
class CopyPlugin {

    public static void copyPlugin(String plugin, String modSource, String modDest) throws IOException {
        String s = File.separator;
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace() + s + "files" + s + "modules.xml");
        String modSFolder = Factory.getWorkspace() + s + xml.getTag("/modules/module[@name=\"" + modSource + "\"]/folder");
        String modDFolder = Factory.getWorkspace() + s + xml.getTag("/modules/module[@name=\"" + modDest + "\"]/folder");

        xml = new XMLPluginConfiguration(modSFolder + s + "plugins.xml");
        String pluginSFolder = Factory.getWorkspace() + s + xml.getTag("/plugins/plugin[@name=\"" + plugin + "\"]/folder");
        String pluginDFolder = modDFolder + s + plugin;

        copyDirectory(new File(pluginSFolder), new File(pluginDFolder));

        //add info to modDFolder/plugins.xml
        xml = new XMLPluginConfiguration(modDFolder + s + "plugins.xml");
        //comprobar si ya existe el plugin en este modulo, para no crear incosistencias
        String ex = xml.getTag("/plugins/plugin[@name=\"" + plugin + "\"]");
        boolean exists = false;
        if (ex != null && ex.length() > 0) {
            exists = true;
        }
        //si ya existe el plugin NO crear nueva informacion
        if (!exists) {
            Element root = xml.getDoc().getRootElement();

            Element n = new Element("plugin");
            n.setAttribute("name", plugin);

            Element folder = new Element("folder");
            pluginDFolder = pluginDFolder.substring(pluginDFolder.indexOf("modules"), pluginDFolder.length());
            folder.setText(pluginDFolder);
            Element config_file = new Element("config_file");
            config_file.setText(pluginDFolder + s + "plugin_configuration.xml");
            n.addContent(folder);
            n.addContent(config_file);
            root.addContent(n);

            xml.getDoc().setRootElement(root);
            xml.dumpFile(modDFolder + s + "plugins.xml");
        }
        //actualizar plugin_configuration.xml de la copia
        xml = new XMLPluginConfiguration(modDFolder + s + plugin+s+"plugin_configuration.xml");
        Element root = xml.getDoc().getRootElement();
        List l = root.getChildren();
        for(int i = 0;i<l.size();i++){
            Element temp = (Element) l.get(i);
            if(temp.getName().equals("module_name")){
                root.removeChild("module_name");
                temp.setText(modDest);
                root.addContent(temp);
                xml.getDoc().setRootElement(root);
                xml.dumpFile(modDFolder + s + plugin+s+"plugin_configuration.xml");
                break;
            }
        }
    }

    private static void copyDirectory(File sourceLocation, File targetLocation)
            throws IOException {

        /*if(!sourceLocation.canRead() || !targetLocation.canWrite()){
        throw new IOException ("can't read or write! (read: "+sourece);
        }*/

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {
            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }

    public static void main(String arg[]) throws IOException {
        Factory.setWorkspace(new File(".").getAbsolutePath());
        CopyPlugin.copyPlugin("LevelGenerator", "Regnum Online", "caca");
    }
}
